package net.chenlin.dp.modules.business.service.impl;

import com.google.common.base.Joiner;
import net.chenlin.dp.common.entity.Page;
import net.chenlin.dp.common.entity.Query;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.utils.CommonUtils;
import net.chenlin.dp.modules.business.dao.TerminalDeviceManageMapper;
import net.chenlin.dp.modules.business.entity.dto.DeviceDto;
import net.chenlin.dp.modules.business.entity.po.DevicePo;
import net.chenlin.dp.modules.business.entity.vo.device.Device;
import net.chenlin.dp.modules.business.entity.vo.device.DeviceInfoVo;
import net.chenlin.dp.modules.business.service.TerminalDeviceManageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Yangsm
 * @ClassName: TerminalDeviceManageServiceImpl
 * @Description: TODO
 * @date 2022年10月13日 15:24
 */
@Service("TerminalDeviceManageService")
public class TerminalDeviceManageServiceImpl implements TerminalDeviceManageService {

    @Autowired
    private TerminalDeviceManageMapper terminalDeviceManageMapper;

    @Override
    public Page<Device> getTerminalList(Map<String, Object> params) {
        Query query = new Query(params);
        Page<Device> page = new Page<>(query);
        List<Device> list =  terminalDeviceManageMapper.getTerminalList(page, query);
        return page;
    }

    @Override
    public R getDeviceInfo(String deviceNo) {
        if(StringUtils.isEmpty(deviceNo)){
            return R.error("设备编号为空");
        }
        DeviceInfoVo deviceInfoVo = terminalDeviceManageMapper.getDeviceList(deviceNo);
        return CommonUtils.msg(deviceInfoVo);
    }

    @Override
    public R createDevice(DeviceDto dto) {
        DevicePo po = new DevicePo();
        po.setCreateId(1L);
        po.setTestNo(dto.getTestNo());
        po.setDeviceNo(dto.getDeviceNo());
        po.setPassNo(dto.getPassNo());
        String join = Joiner.on(",").join(dto.getElectricNo());
        po.setElectricNo(join);
        terminalDeviceManageMapper.insertDevice(po);
        return R.ok("新建成功！");
    }

    @Override
    public R editDevice(DeviceDto dto) {
        DevicePo po = new DevicePo();
        po.setCreateId(1L);
        po.setTestNo(dto.getTestNo());
        po.setDeviceNo(dto.getDeviceNo());
        po.setPassNo(dto.getPassNo());
        String join = Joiner.on(",").join(dto.getElectricNo());
        po.setElectricNo(join);
        po.setId(dto.getId());
        terminalDeviceManageMapper.editDevice(po);
        return R.ok("编辑成功！");
    }

    @Override
    public R getDevice(Long id) {
        Device device = terminalDeviceManageMapper.getDevice(id);
        return CommonUtils.msg(device);
    }

    @Override
    public R deleteDevice(Long id) {
        terminalDeviceManageMapper.deleteDevice(id);
        return R.ok("删除成功！");
    }
}
